package com.bignerdranch.android.technew;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<Bean> mBeanList;
    private Context mContext;
    private LayoutInflater mInflater;
    private onItemClicked mItemClicked;

    public MyAdapter(List<Bean> beanList, Context context,onItemClicked itemClicked) {
        mBeanList = beanList;
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mItemClicked = itemClicked;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item,null);

        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Bean bean = mBeanList.get(position);
        holder.mTextView.setText(bean.getItem());
        holder.mTextView.setTag(position);//组件之间值的传递


    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView mTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.text);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
          /*  Snackbar.make(v,"item is clicked",Snackbar.LENGTH_SHORT)
                    .setAction("Action", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("=====","onClick");
                        }
                    }).show();*/
            int position = (int) mTextView.getTag();
            mItemClicked.onClicked(v,position);
        }
    }
}
